package com.haust.leyou.controller;

import com.haust.leyou.bo.SpuBo;
import com.haust.leyou.pojo.Sku;
import com.haust.leyou.pojo.Spu;
import com.haust.leyou.pojo.SpuDetail;
import com.haust.leyou.service.GoodsService;
import com.haust.leyou.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: csp1999
 * @Date: 2020/12/11/11:05
 * @Description:
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 根据条件分页查询是spu
     *
     * @param key      查询的关键字
     * @param saleable 是否上架：0下架/1上架/null全部商品
     * @param page     当前页
     * @param rows     每页数据数量
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows) {

        PageResult<SpuBo> pageResult = goodsService.querySpuByPage(key, saleable, page, rows);

        if (CollectionUtils.isEmpty(pageResult.getItems())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增商品：需要先后向四张表中增数据
     * 1.tb_spu
     * 2.tb_spu_detail
     * 3.tb_sku
     * 4.tb_stock
     *
     * @param spuBo
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        goodsService.saveGoods(spuBo);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品信息
     *
     * @param spuBo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {

        goodsService.updateGoods(spuBo);

        // 返回状态码203,更新成功！
        return ResponseEntity.noContent().build();
    }


    /**
     * 根据spuId 查询spuDetail: 用于商品编辑的数据回显
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId) {

        SpuDetail spuDetail = goodsService.querySpuDetailBySpuId(spuId);

        if (spuDetail == null) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据spuId 查询sku 集合：用于商品编辑的数据回显
     *
     * @param spuId
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long spuId) {
        List<Sku> skuList = goodsService.querySkusBySpuId(spuId);

        if (CollectionUtils.isEmpty(skuList)) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(skuList);
    }

    /**
     * 根据spuId 查询Spu
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id) {

        Spu spu = goodsService.querySpuById(id);
        if (spu == null) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(spu);
    }

    /**
     * 根据skuId 查询sku
     *
     * @param skuId
     * @return
     */
    @GetMapping("/sku/{skuId}")
    public ResponseEntity<Sku> querySkuBySkuId(@PathVariable("skuId") Long skuId) {
        Sku sku = goodsService.querySkuBySkuId(skuId);

        if (sku == null) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(sku);
    }
}
